package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.aop.UserOperatePower;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.pojo.ApiDataVO;
import com.econ.powercloud.ems.pojo.EmsFaultDitHisVO;
import com.econ.powercloud.ems.pojo.EmsFaultVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsFaultRequestVO;
import com.econ.powercloud.ems.service.biz.FaultService;
import com.econ.powercloud.powernode.gateway.base.utils.CurrentUserUtil;
import com.econ.powercloud.powernode.gateway.common.domain.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zjr
 * @create 2021/7/29 下午5:12
 * @description
 */
@Api(tags = "故障管理")
@RestController
@RequestMapping(value = "/api/v2/fault")
public class FaultController extends ApiBaseController {

    @Autowired
    private FaultService faultService;

    @ApiOperation(value = "获取故障列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "10", dataType = "int")
    })
    @GetMapping(value = "getFaultListByCondition")
    public MsgResult<PageResult<List<EmsFaultVO>>> getFaultListByCondition(EmsFaultRequestVO emsFaultRequestVO,
                                                                           @RequestParam(defaultValue = "1") int pageNo,
                                                                           @RequestParam(defaultValue = "10") int pageSize) {
        UserDTO userDTO = CurrentUserUtil.get();
        return successHandler(faultService.findFaultListByConditionFromClickHouse(userDTO, emsFaultRequestVO, pageNo, pageSize));
    }

    @ApiOperation(value = "获取单个故障详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "故障号", dataType = "String")
    })
    @GetMapping(value = "getFaultInfoById")
    public MsgResult<EmsFaultVO> getFaultInfoById(@RequestParam("id") String id) {
        return successHandler(faultService.selectById(id));
    }

    @ApiOperation(value = "确认处理故障")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "故障号", dataType = "String")
    })
    @PutMapping(value = "modifyFaultById")
    @UserOperatePower
    public MsgResult<String> modifyFaultById(@RequestParam("id") String id) {
        UserDTO userDTO = CurrentUserUtil.get();
        return successHandler(faultService.modifyFaultById(userDTO, id));
    }

    @ApiOperation(value = "一键确认处理故障")
    @PostMapping(value = "modifyFaultByIds")
    @UserOperatePower
    public MsgResult<List<String>> modifyFaultByIds(@RequestBody EmsFaultRequestVO emsFaultRequestVO) {
        UserDTO userDTO = CurrentUserUtil.get();
        return successHandler(faultService.modifyFaultByIds(userDTO, emsFaultRequestVO));
    }


    @ApiOperation(value = "获取平台解释事件对应点号的历史数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", required = true, value = "故障号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", required = false, value = "开始时间", dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "endTime", required = false, value = "结束时间", dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "10  ", dataType = "int")
    })
    @GetMapping(value = "getFaultDitHis")
    public MsgResult<PageResult<List<EmsFaultDitHisVO>>> getFaultYcDitHis(@RequestParam String id,
                                                                          @RequestParam(required = false) Long startTime,
                                                                          @RequestParam(required = false) Long endTime,
                                                                          @RequestParam(value = "pageNo", defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                                          @RequestParam(value = "pageSize", defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize) {
        return successHandler(faultService.getFaultDitHis(id, startTime, endTime, pageNo, pageSize));
    }

    @ApiOperation(value = "获取点号名称和点号标识")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "故障号", dataType = "String")
    })
    @GetMapping(value = "getFaultDit")
    public MsgResult<ApiDataVO> getFaultDit(@RequestParam("id") String id) {
        return successHandler(faultService.getFaultDitAndName(id));
    }

    @ApiOperation(value = "将原来的故障告警记录保存到ClickHouse")
    @GetMapping(value = "syncList")
    public MsgResult<Boolean> syncList() {
        return successHandler(faultService.syncList());
    }
}
